const OnEmitRemoveMan = require('../OnEmitRemoveMan');
const assert = require('simple-assert');
const eql = require('deep-eql');

const oe1 = new OnEmitRemoveMan();

//以下为简单测试
describe(`\n测试传入不接收参数的回调函数`, () => {
    it(`\n测试传入不接收参数的回调函数`, () => {
        const resAry = [];
        oe1.on('push hello', () => {
            resAry.push('hello1');
        });
        oe1.on('push hello', () => {
            resAry.push('hello2');
        });
        oe1.emit('push hello');
        assert(eql(resAry, ['hello1', 'hello2']), `\n测试传入不接收参数的回调函数:`);
    })
});


describe(`\n测试传入接收参数的回调函数`, () => {
    it(`\n测试传入接收参数的回调函数`, () => {
        const resAry = [];
        const pushYourWords = (yourWords) => {
            return () => {
                resAry.push(yourWords);
            }
        };
        oe1.on('push your words', ((wds) => {
            return pushYourWords(wds)
        })('what is your name?'));
        oe1.on('push your words', ((wds) => {
            return pushYourWords(wds)
        })('how are you?'));
        oe1.on('push your words', () => {
            resAry.push('how old are you?')
        });
        oe1.emit('push your words');
        assert(eql(resAry, [
            'what is your name?',
            'how are you?',
            'how old are you?'
        ]), `\n测试传入接收参数的回调函数:`)
    })
});

describe(`\n测试remove方法`, () => {
    it(`\n测试remove方法:`, () => {
        const fnAry = [...new Array(5).keys()].reduce((tmpFnAry, num) => {
            tmpFnAry.push(new Function(`console.log(${num})`))
            return tmpFnAry;
        }, []);
        const bindForRemove = () => fnAry.forEach((fn) => {
            oe1.on('for remove test', fn);
        });
        bindForRemove();
        bindForRemove();
        oe1.emit('for remove test');
        assert(eql([
            ...fnAry, ...fnAry
        ], oe1.planObj['for remove test']), '测试批量绑定函数到"for remove test"自定义事件是否成功');
        oe1.remove('for remove test', fnAry[2]);
        console.log(`removed the function which will output number 2.`);
        oe1.emit('for remove test');
        assert(eql([
            ...fnAry.filter(item => item !== fnAry[2]),
            ...fnAry.filter(item => item !== fnAry[2])
        ], oe1.planObj['for remove test']), '测试用remove方法删除"for remove test"自定义事件中绑定的fnAry[2]是否成功');
    });
});

describe(`\n测试removeFirst方法`, () => {
    it(`\n测试removeFirst方法`, () => {
        console.log(`\n测试removeFirst方法:`);
        const fnAry = [...new Array(5).keys()].reduce((tmpFnAry, num) => {
            // console.log(tmpFnAry, 565656);
            tmpFnAry.push(new Function(`console.log(${num})`))
            // console.log(tmpFnAry,585858);
            return tmpFnAry;
        }, []);
        const bindForRemoveFirst = () => fnAry.forEach((fn) => {
            oe1.on('for removeFirst test', fn);
        });
        bindForRemoveFirst();
        bindForRemoveFirst();
        assert(eql([
            ...fnAry, ...fnAry
        ], oe1.planObj['for removeFirst test']), '测试批量绑定函数到"for remove test"自定义事件是否成功');
        oe1.emit('for removeFirst test');
        oe1.removeFirst('for removeFirst test', fnAry[2]);
        //下一行是用来起分割线作用的如果注销,这有时不会输出,可能是mocha测试框架认为不需要输出, 如果是其他情况请告诉我2838309423@qq.com
        console.log(`removed the first function which will output number 2.`);
        oe1.emit('for removeFirst test');
        assert(eql([
            ...fnAry.filter(item => item !== fnAry[2]),
            ...fnAry
        ], oe1.planObj['for removeFirst test']), '测试用removeFirst方法删除"for removeFirst test"自定义事件中绑定的第一个fnAry[2]是否成功');
    });
});
